package com.creditease.mobile.rss.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.creditease.mobile.R;
import com.creditease.mobile.rss.model.RssModel;
import com.creditease.mobile.util.LruCacheUtil;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class RssAdapter extends BaseAdapter {

    private static final String TAG = RssAdapter.class.getName();

    private Context context;

    private List<RssModel> list;

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public RssAdapter(Context context, List<RssModel> list) {
        super();
        this.context = context;
        this.list = list;
    }

    ConcurrentHashMap<Integer, View> viewHold = new ConcurrentHashMap<Integer, View>();

    public void updateView() {
        viewHold.clear();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        convertView = viewHold.get(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.home_item,
                    null);
            RssModel itemModel = list.get(position);
            ImageView logo = (ImageView) convertView.findViewById(R.id.rss_logo);
            TextView title = (TextView) convertView.findViewById(R.id.rss_title);
            TextView desc = (TextView) convertView.findViewById(R.id.rss_desc);

            title.setText(itemModel.title);
            desc.setText(itemModel.desc);
            // Log.d(TAG, "isRead="+itemModel.isRead);
            String key = "rssModel" + itemModel.isRead;
            if (itemModel.isRead) {
                Bitmap bitmap = LruCacheUtil.getBitmapFromMemCache(key);
                if (bitmap == null) {
                    bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_rss_read);
                    LruCacheUtil.addBitmapToCache(key, bitmap);
                }
                logo.setImageBitmap(bitmap);
            } else {
                Bitmap bitmap = LruCacheUtil.getBitmapFromMemCache(key);
                if (bitmap == null) {
                    bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_rss);
                    LruCacheUtil.addBitmapToCache(key, bitmap);
                }
                logo.setImageBitmap(bitmap);
            }
            viewHold.put(position, convertView);
        }
        return convertView;
    }
}
